package cn.edu.nuc.ehelper;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.nuc.wordsbook.Constant;
import cn.edu.nuc.wordsbook.Parse;
import cn.edu.nuc.wordsbook.SQLiteOperationHelper;
import cn.edu.nuc.wordsbook.WordsBook;

public class ReviewActivity extends Activity {

	private TextView wordsContent;
	private Button notRemember;
	private Button remember;

	private SQLiteOperationHelper dbHelper;
	public static List<WordsBook> wordsBook;
	private WordsBook words = new WordsBook();
	private static int position = 0;
	private static int flag = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.review);
		ReviewActivity.this.setTitle("复习");
		position = 0;

		dbHelper = new SQLiteOperationHelper(ReviewActivity.this,
				Constant.TABLEName, null, 1);
		wordsContent = (TextView) findViewById(R.id.reviewWordsViewId);
		notRemember = (Button) findViewById(R.id.NotRememberButtonId);
		remember = (Button) findViewById(R.id.RememberButtonId);
		fetchMessages(ReviewActivity.this);
		if (wordsBook.isEmpty()) {
			wordsContent.setText("无");
			Toast.makeText(ReviewActivity.this, "单词本为空", Toast.LENGTH_SHORT).show();
			finish();
		}else {
			words = wordsBook.get(position);
			wordsContent.setText(words.english);
			
		}

		notRemember.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (flag == 0) {
					// words = wordsBook.get(position);
					wordsContent.setText(words.english + "\n"
							+ words.translation);
					notRemember.setText("下次再学");
					flag = 1;
				} else if (flag == 1) {
					position++;
					if (position >= (wordsBook.size() )) {
						new AlertDialog.Builder(ReviewActivity.this)
						.setTitle("背诵完成")
						.setPositiveButton("结束",
								new DialogInterface.OnClickListener() {
							public void onClick(
									DialogInterface dialog,
									int whichButton) {
								setResult(RESULT_OK);
								finish();
							}
						}).setNegativeButton("取消", null).show();

						position = wordsBook.size() - 1;
					}
					words = wordsBook.get(position);
					wordsContent.setText(words.english);
					notRemember.setText("不记得");
					flag = 0;
				}
			}
		});

		remember.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				position++;
				if (position >= (wordsBook.size())) {
					new AlertDialog.Builder(ReviewActivity.this)
					.setTitle("背诵完成")
					.setPositiveButton("结束",
							new DialogInterface.OnClickListener() {
						public void onClick(
								DialogInterface dialog,
								int whichButton) {
							setResult(RESULT_OK);
							finish();
						}
					}).setNegativeButton("取消", null).show();

					position = wordsBook.size() - 1;
				}
				ContentValues values = new ContentValues();
				long millDate = System.currentTimeMillis();
				String date = DateFormat.format("yyyy-MM-dd", millDate)
						.toString();
				values.put("lastdate", date);
				values.put("millDate", millDate);
				System.out.println(words.id);
				dbHelper.update(Long.parseLong(words.id), values);

				words = wordsBook.get(position);
				wordsContent.setText(words.english);

			}
		});
	}

	public Cursor getDb(Context context) {
		Cursor c = dbHelper.query("milldate asc");
		return c;
	}

	private void fetchMessages(Context context) {
		if (wordsBook == null) {
			wordsBook = new ArrayList<WordsBook>();
		} else {
			wordsBook.clear();
		}
		Cursor c = getDb(context);

		startManagingCursor(c);
		if (c != null) {
			if (c.moveToFirst()) {
				do {
					WordsBook wb = Parse.parseCursor(c);
					wordsBook.add(wb);
				} while (c.moveToNext());
			}
		}
//		c.close();
		// dbHelper.close();
	}
}
